package com.situ.mvc.controller.text;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entity.Student;
//地址/student/insert.action,,,相当于web.xml里面的地址
@Controller
@RequestMapping(value="/studenttext")
public class StudentControllerText {
	
	@RequestMapping(value="/getInsertPage")  //from表单提交跳转的地址
	public String getInsertPage(){
		//return "/WEB-INF/jsp/student_insert.jsp";
		return "student_insert";
	}
	
	//ModelAndView方式
	@RequestMapping(value="/insert")//from表单要跳转的地址
	public ModelAndView insert(Student student){   //相当于new出来的student对象
		System.out.println(student);
		//相当与于request.setAttribute("student", student);
		//Model域对象放数据，View处理转发和重定向界面
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("student",student);
		//重定向
		//相当于request.getRequestDespatcher("/student_info.jsp").forwar(req,resp);
		modelAndView.setViewName("student_info");
		return modelAndView;
	}
	
	//String方式的***
	@RequestMapping(value="/insert1" , method=RequestMethod.POST)//采用post方式
	public String insert1(Student student,Model model){
		System.out.println(student);
		model.addAttribute("student",student);
		return "student_info";
	}
	
	@RequestMapping(value="/insert6",method=RequestMethod.POST)
	public String insert6(Student student,Model model) {
		System.out.println(student);
		model.addAttribute("student",student);
		return "student_info";
	}
	
	//----------------------------------------------------------
	//传统方法  request respoinse
	@RequestMapping(value="/insert2" , method=RequestMethod.POST)//采用post方式
	public void insert2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		Integer age = Integer.parseInt(request.getParameter("age"));
		String gender = request.getParameter("gender");
		Student student = new Student(null, name, age, gender);
		request.setAttribute("student",student);
		request.getRequestDispatcher("/WEB-INF/jsp/student_info.jsp").forward(request, response);;
	}
	//------------------------------------------------------------
	//在业务控制写入普通变量参数,就是把Student封装的数据挨个拿出来
	@RequestMapping(value="/insert3",method=RequestMethod.POST)
	public String insert3(String name, Integer age,String gender,Model model){
		System.out.println(name);
		System.out.println(age);
		System.out.println(gender);
		
		model.addAttribute("name",name);
		model.addAttribute("age",age);
		model.addAttribute("gender",gender);
		return "student_info";
	}
	
	//------------------------------------------------------------
	//模拟id删除，直接输入网址
	// student/deleteById.action?id=8
	@RequestMapping(value="/deleteById")
	public String deleteById(Integer id, Model model) {
	   System.out.println("id:" + id);
	   model.addAttribute("id", id);
	   return "student_info";
	}
	//-----------------------------------------------------------
	//进入删除页面
	@RequestMapping(value="/todelete")
	public String todelete() {
		return "student_deleteall";
	}
	
	//批量删除拿到id
	@RequestMapping(value="/deleteAll")
	public String deleteAll(Integer[] ids) {
		for (Integer integer : ids) {
			System.out.println(ids);
		}
		System.out.println(Arrays.toString(ids));
		//return "student_info";//转发
		return "redirect:/student/pageList.action";
	}
	
	//-------------------------------------------------------------
	//重定向
	@RequestMapping(value="/pageList")
	public String pageList() {
		System.out.println("StudentController.pageList()");
		return "student_info";
	}
}
